package com.dgut.service.serviceImpl;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.common.SelectRole;
import com.dgut.common.StateForUser;
import com.dgut.mapper.AdministratorMapper;
import com.dgut.mapper.BuyerMapper;
import com.dgut.mapper.UserMapper;
import com.dgut.pojo.Administrator;
import com.dgut.pojo.Buyer;
import com.dgut.pojo.User;
import com.dgut.pojo.receive.AdministratorAndBuyerReceive;
import com.dgut.service.AdministratorService;
import com.dgut.utils.PageRequestInfo;
import com.dgut.utils.PageResult;
import com.dgut.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@SuppressWarnings({"all"})
@Service
public class AdministratorServiceImpl implements AdministratorService {
    @Resource
    private AdministratorMapper administratorMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private BuyerMapper buyerMapper;


    /**
     * start-zwb
     */
    /**
     * end-zwb
     */


    /**
     * start-gxd
     */

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object blockBuyer(int id) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Buyer buyer = buyerMapper.selectById(id);
        buyer.setState(StateForUser.BLOCKED);
        int update = buyerMapper.updateById(buyer);//冻结采买方用户
        if (update >= 0){
            resultMap.put("result",true);
            resultMap.put("message","用户已冻结！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","冻结失败！");
        }
        //3.将用户踢下线
        StpUtil.logoutByLoginId(id);
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.ADMIN)//角色认证
    public Object unBlockBuyer(int id) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Buyer buyer = buyerMapper.selectById(id);
        buyer.setState(StateForUser.NORMAL);
        int update = buyerMapper.updateById(buyer);//解冻采买方用户
        if (update >= 0){
            resultMap.put("result",true);
            resultMap.put("message","用户已解冻！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","解冻失败！");
        }
        return resultMap;
    }

    /**
     * end-gxd
     */


    /**
     * start-lzh
     */
    /**
     * end-lzh
     */

    /**
     * start-hwf
     */

    @Override
    public Administrator selectAdministratorById(int id) {
        return administratorMapper.selectById(id);
    }

    @Override
    public boolean addAdmin(AdministratorAndBuyerReceive administratorAndBuyerReceive) {
        if(administratorAndBuyerReceive.getUsername() == null && administratorAndBuyerReceive.getPassword() == null){
            return false;
        }
        Map map = new Hashtable();
        map.put("username",administratorAndBuyerReceive.getUsername());
        List<User> list = userMapper.selectByMap(map);
        if(list.size() != 0){
            return false;
        }else {
            User user = new User();
            Administrator administrator = new Administrator();

            user.setRole(SelectRole.ADMIN);
            user.setUsername(administratorAndBuyerReceive.getUsername());
            user.setPassword(administratorAndBuyerReceive.getPassword());
            userMapper.insert(user);

            administrator.setAddress(administratorAndBuyerReceive.getAddress());
            administrator.setId(user.getId());
            administrator.setBirthDate(administratorAndBuyerReceive.getBirthDate());
            administrator.setEmail(administratorAndBuyerReceive.getEmail());
            administrator.setHeadPortrait(administratorAndBuyerReceive.getHeadPortrait());
            administrator.setIdCard(administratorAndBuyerReceive.getIdCard());
            administrator.setName(administratorAndBuyerReceive.getName());
            administrator.setNickname(administratorAndBuyerReceive.getNickname());
            administrator.setPhone(administratorAndBuyerReceive.getPhone());
            administrator.setSex(administratorAndBuyerReceive.getSex());
            administrator.setState(StateForUser.NORMAL);
            administratorMapper.insert(administrator);

            return true;
        }
    }

    @Override
    public PageResult normalAdminPage(PageRequestInfo pageRequestInfo) {
        Page<Administrator> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<Administrator> resultPage = administratorMapper.selectPage(page, Wrappers.<Administrator>lambdaQuery().eq(Administrator::getState,StateForUser.NORMAL));
        return PageUtils.getPageResult(resultPage);
    }

    @Override
    public PageResult blockedAdminPage(PageRequestInfo pageRequestInfo) {
        Page<Administrator> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<Administrator> resultPage = administratorMapper.selectPage(page,Wrappers.<Administrator>lambdaQuery().eq(Administrator::getState,StateForUser.BLOCKED));
        return PageUtils.getPageResult(resultPage);
    }

    @Override
    public PageResult adminPage(PageRequestInfo pageRequestInfo) {
        Page<Administrator> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<Administrator> resultPage = administratorMapper.selectPage(page,null);
        return PageUtils.getPageResult(resultPage);
    }

    @Override
    public Administrator getAdmin(int id) {
        return administratorMapper.selectById(id);
    }

    @Override
    public boolean updateAdmin(AdministratorAndBuyerReceive administratorAndBuyerReceive) {
        Administrator administrator = new Administrator();

        administrator.setAddress(administratorAndBuyerReceive.getAddress());
        administrator.setId(administratorAndBuyerReceive.getId());
        administrator.setBirthDate(administratorAndBuyerReceive.getBirthDate());
        administrator.setEmail(administratorAndBuyerReceive.getEmail());
        administrator.setHeadPortrait(administratorAndBuyerReceive.getHeadPortrait());
        administrator.setIdCard(administratorAndBuyerReceive.getIdCard());
        administrator.setName(administratorAndBuyerReceive.getName());
        administrator.setNickname(administratorAndBuyerReceive.getNickname());
        administrator.setPhone(administratorAndBuyerReceive.getPhone());
        administrator.setSex(administratorAndBuyerReceive.getSex());
        administrator.setState(administrator.getState());

        if(administratorMapper.updateById(administrator) >= 1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean changeAdminState(int id,String state) {
        if(administratorMapper.blockAdmin(id,state) >= 1){
            return true;
        }else{
            return false;
        }
    }

    @Override
    @Transactional
    public boolean deleteAdmin(int id) {
        if(administratorMapper.deleteById(id) >= 1 && userMapper.deleteById(id) >=1){
            return true;
        }else{
            return false;
        }
    }
    /**
     * end-hwf
     */
}
